home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 304.adf / Verify / verify.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  7KB  |  306 lines

  1. /**********************************************************************/
  2. /*                                                                    */
  3. /* verify - Version 1.2                                               */
  4. /*                                                                    */
  5. /* January 15 1989 - Joel Swank
  6. /* This program is freely copyable and distributable.                 */
  7. /*                                                                    */
  8. /* Directory Search taken from Rodney Lewis' Find program.            */
  9. /*                                                                    */
  10. /*                                                                    */
  11. /**********************************************************************/
  12.  
  13. /**********************************************************************/
  14. /*                                                                    */
  15. /* verify  - searches the directory hierachy reading all files.  Any  */
  16. /*           files that cannot be read in entirity are reported on    */
  17. /*           stdout.                                                  */
  18. /*                                                                    */
  19. /**********************************************************************/
  20.  
  21. #include <exec/types.h>
  22. #include <exec/memory.h>
  23. #include <libraries/dosextens.h>
  24. #include <stdio.h>
  25. #include <functions.h>
  26. #include <fcntl.h>
  27. #include "verify.h"
  28.  
  29. #define BUFSIZE 8192L
  30.  
  31. int breakflag = FALSE;
  32.  
  33. char *buf = NULL;
  34. char path[80] = "";                /* memory to hold full path name */
  35. char vfmsg[] = "Verify Fail: ";
  36. int goodct = 0, badct = 0;
  37.  
  38.  
  39. main(argc, argv)
  40. int argc;
  41. char *argv[];
  42. {
  43.     register struct FileLock *start;
  44.     register i;
  45.  
  46.     if (argc < 2) {
  47.         fprintf(stderr,"Usage:verify path . . .\n");
  48.         fprintf(stderr,"Read all files in all directories\n");
  49.         exit(1);
  50.         }
  51.  
  52.     /* search each path-name specified */
  53.  
  54.     for (i = 1 ; i < argc; i++)
  55.         {
  56.         if (argv[i][0] == '.' && argv[i][1] == '\0')
  57.             {        /* add '.' for cwd JHS 9-21-88 */
  58.             struct Process *MyProcess;
  59.             MyProcess = (struct Process *) FindTask(0L);
  60.             start = DupLock(MyProcess->pr_CurrentDir);
  61.             }
  62.         else start = Lock(argv[i], ACCESS_READ);
  63.         if (start == NULL) {
  64.             fprintf(stderr, "verify: can't access '%s'\n", argv[i]);
  65.             continue;
  66.             }
  67.  
  68.         search(start);
  69.         pwd(start);
  70.         fprintf(stdout,"%s\n",path);
  71.         fprintf(stdout,"%d Good Files\n",goodct);
  72.         fprintf(stdout,"%d Bad Files\n",badct);
  73.         goodct = 0; badct = 0;
  74.         path[0] = '\0';
  75.         UnLock(start);
  76.         }
  77.  
  78.     if (buf)
  79.         FreeMem(buf, BUFSIZE);
  80.     exit(0);
  81. }
  82.  
  83. /* search the given directory and read each file
  84.  * 
  85.  */
  86.  
  87. search(lock)
  88. register struct FileLock *lock;
  89. {
  90.     register struct FileInfoBlock *fib;
  91.     register struct FileLock *nlock = NULL, *par = NULL;
  92.     char *prev, file[200];
  93.     int Err;
  94.  
  95.     fib = (struct FileInfoBlock *) AllocMem((long)
  96.         sizeof(struct FileInfoBlock), MEMF_CLEAR);
  97.     if (fib == NULL) {
  98.         fprintf(stderr, "verify: can't allocate file info block\n");
  99.         return(0);
  100.     }
  101.  
  102.     /* save current position in full path name */
  103.  
  104.     prev = path + strlen(path);
  105.  
  106.     if (*path == '\0' && pwd(par = ParentDir(lock)) == 0) {
  107.         FreeMem(fib, (long) sizeof(struct FileInfoBlock));
  108.         return(0);
  109.     }
  110.  
  111.     if (par) UnLock(par); /* JHS 12/3/89 */
  112.  
  113.     /* examine initial path name */
  114.  
  115.     if (Examine(lock, fib)) {
  116.  
  117.  
  118.         if (fib->fib_DirEntryType > 0) {
  119.  
  120.             /* set up printable path name */
  121.  
  122.             if (*path) {
  123.                 strcat(path, fib->fib_FileName);
  124.                 strcat(path, "/");
  125.             }
  126.             else if (pwd(lock) == 0) {
  127.                 *prev = '\0';
  128.                 FreeMem(fib, (long) sizeof(struct FileInfoBlock));
  129.                 return(0);
  130.             }
  131.         }
  132.  
  133.         else {
  134.  
  135.             /* if initial path name is not a directory then we just return */
  136.  
  137.             *prev = '\0';
  138.             FreeMem(fib, (long) sizeof(struct FileInfoBlock));
  139.             fprintf(stderr, "verify: not a directory - %s\n", file);
  140.             return(0);
  141.         }
  142.  
  143.         /* examine directory contents */
  144.  
  145.         while(ExNext(lock, fib)) {
  146.             if (breakflag) break;
  147.  
  148.             /* recurse if we have found a directory */
  149.  
  150.             strcpy(file, path);
  151.             strcat(file, fib->fib_FileName);
  152.             if (fib->fib_DirEntryType > 0) {
  153.                 nlock = Lock(file, ACCESS_READ);
  154.                 if (nlock == NULL)
  155.                     fprintf(stderr, "verify: locking error - %s\n", file);
  156.                 else {
  157.                     search(nlock);
  158.                     UnLock(nlock);
  159.                 }
  160.             }
  161.             else
  162.                 read_file(file);
  163.  
  164.             if (SetSignal(0L, 0L) & SIGBREAKF_CTRL_C) {
  165.                 breakflag = TRUE;
  166.                 break;
  167.             }
  168.         }
  169.         Err = (int) IoErr();
  170.         if (Err != ERROR_NO_MORE_ENTRIES)
  171.             fprintf(stderr, "verify: directory error - %s\n", path);
  172.     }else 
  173.         fprintf(stderr, "verify: directory error - %s\n", path);
  174.  
  175.  
  176.     *prev = '\0';
  177.     FreeMem(fib, (long) sizeof(struct FileInfoBlock));
  178. }
  179.  
  180. /*
  181.  *  read the file described by path and fib->fib_FileName
  182.  */
  183.  
  184. read_file(file)
  185. char *file;
  186. {
  187.     int ret;
  188.       struct FileHandle *f1;
  189.  
  190.     if (buf == NULL) {    /* get buffer first time only */
  191.         buf = (char *)AllocMem(BUFSIZE, MEMF_PUBLIC|MEMF_CLEAR);
  192.         if (buf == NULL) {
  193.             fprintf(stderr,"verify: No memory for buffers\n");
  194.             return;
  195.             }
  196.         }
  197.  
  198.  
  199.     f1 = Open(file, MODE_OLDFILE);
  200.  
  201.     if (f1 == NULL) {
  202.         fputs(vfmsg,stderr);
  203.         ierror(file,205);
  204.         return;
  205.         badct++;
  206.         }
  207.  
  208.     while ((ret = Read(f1, buf, BUFSIZE)) > 0L );
  209.  
  210.     if (ret < 0)
  211.         {
  212.         int Err = (int) IoErr();
  213.         fputs(vfmsg,stderr);
  214.         ierror(file, Err);
  215.         badct++;
  216.         }
  217.     else  goodct++;
  218.  
  219.     Close(f1);
  220.  
  221.  
  222. }
  223.  
  224.  
  225. /* find the full path name of the given lock */
  226.  
  227. pwd(dir)
  228. register struct FileLock *dir;
  229. {
  230.     register struct FileLock *par = NULL;
  231.     register struct FileInfoBlock *fib;
  232.  
  233.     if (dir == NULL) {
  234.         *path = '\0';
  235.         return(1);
  236.     }
  237.  
  238.     fib = (struct FileInfoBlock *) AllocMem((long)
  239.             sizeof(struct FileInfoBlock), MEMF_CLEAR);
  240.     if (fib == NULL) {
  241.         fprintf(stderr, "verify: can't allocate a FileInfoBlock\n");
  242.         return(0);
  243.     }
  244.  
  245.     if (!Examine(dir, fib)) {
  246.         fprintf(stderr, "verify: examine failed\n");
  247.         FreeMem(fib, (long) sizeof(struct FileInfoBlock));
  248.         return(0);
  249.     }
  250.  
  251.     if (par = ParentDir(dir)) {
  252.  
  253.         /* find full path name of parent */
  254.  
  255.         if (pwd(par) == 0) {
  256.             UnLock(par); /* JHS 12/3/89 */
  257.             FreeMem(fib, (long) sizeof(struct FileInfoBlock));
  258.             return(0);
  259.         }
  260.  
  261.         /* add current name */
  262.  
  263.         strcat(path, fib->fib_FileName);
  264.         strcat(path, "/");
  265.         UnLock(par); /* JHS 12/3/89 */
  266.     }
  267.     else {
  268.  
  269.         /* found the root name */
  270.  
  271.         strcpy(path, fib->fib_FileName);
  272.         strcat(path, ":");
  273.     }
  274.  
  275.     FreeMem(fib, (long) sizeof(struct FileInfoBlock));
  276.     return(1);
  277. }
  278.  
  279.  
  280. /*
  281.  *   Matt Dillons error routines
  282.  */
  283.  
  284.  
  285. ierror(str, err)
  286. register char *str;
  287. int err;
  288. {
  289.    register struct PERROR *per = Perror;
  290.  
  291.    if (err > 0) {
  292.       for (; per->errstr; ++per) {
  293.          if (per->errnum == err) {
  294.             fprintf (stderr, "%s%s%s\n",
  295.               per->errstr,
  296.               (str) ? ": " : "",
  297.               (str) ? str : "");
  298.             return;
  299.         }
  300.       }
  301.       fprintf (stderr, "Unknown DOS error %d %s\n", err, (str) ? str : "");
  302.    }
  303.    return;
  304. }
  305.  
  306.